<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">
	<head>
		<meta charset="utf-8">
		<title>药品采购单</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
	</head>
	<body>
		<!-- 相对于from表单 -->
		<div class="layui-form layui-form-pane" lay-filter="form-layfilter-recovery" id="form-layfilter-recovery" style="padding: 10px 10px 0 10px;">
			<div class="layui-form-item" th:hidden="true"><!--  -->
				<label class="layui-form-label">编号</label>
				<div class="layui-input-inline">
					<input type="text" name="re_id" th:value="${po!=null?po.re_id:''}" readonly="readonly" placeholder="不可自定义"
					 autocomplete="off" class="layui-input layui-disabled">
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">回收时间</label>
				<div class="layui-input-inline">
					<input type="text" name="re_recoverytime" th:value="${po!=null ? #dates.format(po.re_recoverytime,'yyyy-MM-dd HH:mm:ss') : ''}" id="re_recoverytime" 
						lay-verify="required" placeholder="必填" class="layui-input">
				</div>
				<label class="layui-form-label">申请人</label>
				<div class="layui-input-inline">
					<select name="re_applicantid" lay-verify="required" lay-verType="tips">
						<option value="">必填</option>
						<option th:each="item:${users}" th:value="${item.user_id}" th:selected="${po!=null&&po.re_applicantid==item.user_id ? true : false}">[[${item.realname}]]</option>
					</select>
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">搜索药品</label><!--   -->
				<div class="layui-input-inline" style="width: 500px;">
					<input type="text" id="drugDict_name" oninput="search(this)" th:value="${po!=null&&po.drug!=null?po.drug.drugDict_name:''}"  class="layui-input" 
						style="position:absolute;z-index:2;width:91%;" placeholder="模糊搜索" autocomplete="off">
                    <select type="text" id="SelectId-drugDict_name" lay-filter="drugDict_name-lay-filter" autocomplete="off" class="layui-select" lay-search>
                        <option value="">清空</option>
                        <option th:each="item:${drugs}" th:value="${item.drugDict_id}" th:selected="${po!=null?po.drug.drugDict_id==item.drugDict_id:false}">[[${item.drugDict_name}]]</option>
                    </select>
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">选择批次</label>
				<div class="layui-input-inline" style="width: 500px;">
					<input type="text" id="re_is_id" oninput="search(this)" th:value="${po!=null&&po.instock!=null?po.instock.is_batch:''}" class="layui-input" placeholder="模糊搜索" autocomplete="off">
                    <select type="text" name="re_is_id" id="SelectId-re_is_id" lay-filter="TextSelect-lay-filter" lay-verify="required" lay-verType="tips"
                    	autocomplete="off" class="layui-select" lay-search>
                        <option value="">必填</option>
                        <option th:value="${po!=null&&po.instock!=null?po.instock.is_id:''}" th:selected="${po!=null&&po.instock!=null?true:false}">[[${po!=null&&po.instock!=null?po.instock.is_batch:''}]]</option>
                    </select>
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">数量</label>
				<div class="layui-input-inline">
					<input type="text" name="re_num" th:value="${po!=null?po.re_num:''}" lay-verify="gtZero" lay-verType="tips" placeholder="必填" autocomplete="off" class="layui-input">
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">来源</label>
				<div class="layui-input-inline">
					<select name="re_source" lay-filter="re_source-lay-filter" lay-verify="required" lay-verType="tips">
						<option value="">必填</option>
						<option th:each="item:${sources}" th:value="${item}" th:selected="${po!=null&&po.re_source==item.name() ? true : false}">[[${item}]]</option>
					</select>
				</div>
				<div id="re_fromstorehouse-box">
					<div th:if="${po!=null&&po.re_source=='来自库房' ? true : false }">
						<label class="layui-form-label">来自库房</label>
						<div class="layui-input-inline">
							<input type="text" th:value="${po!=null&&po.fromstorehouse!=null? po.fromstorehouse.store_name : ''}" placeholder="不可自定义" class="layui-input layui-disabled" readonly="readonly">
						</div>
					</div>
				</div>
				<div id="re_frompharmacyid-box" th:hidden="${po!=null&&po.re_source=='来自药房' ? false : true }">
					<label class="layui-form-label">来自药房</label>
					<div class="layui-input-inline">
						<select name="re_frompharmacyid" data-th-lay-verify="${po!=null&&po.re_source=='来自药房'?'required':''}" data-th-lay-verType="${po!=null&&po.re_source=='来自药房'?'tips':''}">
							<option value="">必填</option>
							<option th:each="item:${pharmacies}" th:value="${item.pharmacy_id}" th:selected="${po!=null&&po.re_frompharmacyid==item.pharmacy_id ? true : false}">[[${item.pharmacy_id}]]</option>
						</select>
					</div>
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">原因</label>
				<div class="layui-input-inline">
					<select name="re_reason" lay-verify="required" lay-verType="tips">
						<option value="">必填</option>
						<option th:each="item:${reasons}" th:value="${item}" th:selected="${po!=null&&po.re_reason==item.name() ? true : false}">[[${item}]]</option>
					</select>
				</div>
				<label class="layui-form-label">说明</label>
				<div class="layui-input-inline">
					<input type="text" name="re_remark" th:value="${po!=null?po.re_remark:''}" placeholder="可选" autocomplete="off" class="layui-input">
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">回收到</label>
				<div class="layui-input-inline">
					<select name="re_toroomid" lay-verify="required" lay-verType="tips">
						<option value="">必填</option>
						<option th:each="item:${storehouses}" th:value="${item.store_id}" th:selected="${po!=null&&po.re_toroomid==item.store_id ? true : false}">[[${item.store_name}]]</option>
					</select>
				</div>
			</div>
			
			<div class="layui-form-item" th:hidden="${po!=null&&po.confirm!=null?false:true}"><!-- th:hidden="${po!=null&&po.confirm!=null?true:false}" -->
				<label class="layui-form-label" style="visibility: hidden;"><span>&nbsp;</span></label>
				<div class="layui-input-inline">
					<span>&nbsp;</span>
				</div>
				<label class="layui-form-label">验收人</label>
				<div class="layui-input-inline">
					<input type="text" th:value="${po!=null&&po.confirm!=null?po.confirm.realname:''}" placeholder="不可自定义" autocomplete="off" class="layui-input layui-disabled" readonly="readonly">
				</div>
			</div>
			
			<!-- 不知道有什么用 -->
			<div class="layui-form-item layui-hide">
				<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
				<input type="button" lay-submit lay-filter="LAY-recovery-front-submit" id="LAY-recovery-front-submit" value="确认">
			</div>
		</div>
		<!-- 引入layui框架 -->
		<script th:src="@{/layuiadmin/layui/layui.js}"></script>
		<!-- 引入公共layui代码 -->
		<script type="text/javascript" th:src="@{/public/warehouse/publicsetting.js}"></script>
		<script>
			//启动layui语法：config全局配置
			layui.config({
				base: '[[@{/layuiadmin/}]]' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'upload','laydate'], function() {//启用哪些组件
				//生成标签的对应对象
				var $ = layui.$,
					form = layui.form,
					upload = layui.upload;
				//不知道是什么
				upload.render({
					elem: '#layuiadmin-upload-useradmin',
					url: layui.setter.base + 'json/upload/demo.js',
					accept: 'images',
					method: 'get',
					acceptMime: 'image/*',
					done: function(res) {
						$(this.item).prev("div").children("input").val(res.data.src)
					}
				});
				
				//启动绑定时间组件
				var laydate = layui.laydate;
				laydate.render({
					elem: "#re_recoverytime", //指定元素
					type: "datetime",//指定类型
					//format: 'yyyy-MM-dd',//自定义格式
				});
				
				//监听下拉框选择事件
				form.on('select(drugDict_name-lay-filter)', function (data) {
					console.log(data);
					var key=data.elem.id.substring(9,data.elem.id.length);//SelectId-;TextId-
				    if (data.value == "") {//当option的value值等于某值时
				        $("#"+key).val("");//把下拉框的value值赋给输入框
				    }else{
				    	$("#"+key).val(data.elem[data.elem.selectedIndex].text);//把下拉框的内容值赋给输入框
				    	//$("#"+key+"TextId").val($("#"+key+"SelectId option:selected").text());//把下拉框的内容值赋给输入框
				   
				    	//异步请求入库单，获得批次信息
				    	$.post("[[@{/warehouse/drugstock/queryByDrugId}]]", { 
				    			"id": data.value,
				    		},
			    		    function (data) {
				    			var list=data.data;
				    			console.log(data);
				    			//没有数据时，还是要清空
				    			$("#SelectId-re_is_id").html("");
			    				$("#SelectId-re_is_id").append("<option value=''>清空</option>");
				    			if(list!=null && list.length>0){
				    				for(var i=0;i<list.length;i++){
				    					//拼接下拉框
				    					var option="<option value='"+list[i].ds_is_id+"'>"+list[i].storehouse.store_name+" --- "+list[i].instock.is_batch+"</option>";
				    					$("#SelectId-re_is_id").append(option);
				    				}
				    				//重新渲染下拉框
				    				form.render("select");
				    				layer.msg(data.msg);
				    			}else{
				    				layer.alert(data.msg);
				    			}
			    		    }, "json");
				    }
				    $("#"+key).next().find("dl").css({//隐藏下拉框的选项
				        "display": "none"
				    });
				   
				   form.render();//重新渲染下拉框
				});
				//监听来源下拉框
				form.on('select(re_source-lay-filter)', function (data) {
					//隐藏来自库房区域
					$("#re_fromstorehouse-box").hide();
					
					//隐藏来自药房区域
					$("#re_frompharmacyid-box").hide();
					//来自药房下拉框不用必填
					$("[name='re_frompharmacyid']").attr("lay-verify","").attr("lay-verType","");
					
					//当选择来自药房时，设置药房下拉框必填
					if(data.value=="来自药房"){
						//显示来自药房区域
						$("#re_frompharmacyid-box").show();
						//设置来自药房下拉框必填
						$("[name='re_frompharmacyid']").attr("lay-verify","required").attr("lay-verType","tips");
					}else if(data.value=="来自库房"){
						//显示来自药房区域
						$("#re_fromstorehouse-box").show();
					}
					
					form.render();//重新渲染下拉框
				});
			})
		</script>
	</body>
</html>
